//
//  OrderDiscountViewController.m
//  danpin
//
//  Created by chuangjia on 30/11/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "OrderDiscountViewController.h"
#import "OrderApi.h"
#import "ZShare.h"
@interface OrderDiscountViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
}
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)int selectIndex;
@property(nonatomic,strong)UITextField *giftNum;
@end

@implementation OrderDiscountViewController
@synthesize giftNum=_giftNum;
@synthesize totalfee=_totalfee;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle =@"使用优惠券";
    _selectIndex=0;
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    [self initUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc
{
    self.giftNum = nil;
    self.actionBlock = nil;
}
-(void)setCallBack:(void(^)(UIView*,id))callback
{
    self.actionBlock =callback;
}
-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, hightAuto(65), self.view.bounds.size.width, self.view.bounds.size.height-hightAuto(65)) style:UITableViewStylePlain];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor =[UIColor clearColor];
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbView.showsVerticalScrollIndicator = NO;
    _tbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tbView];
    
}
#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return 80;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.backgroundColor=[UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    UIView *gift =[self giftview];
    [cell addSubview:gift];
    gift = nil;
    return cell;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(UIView *)giftview
{
    UIView *giftv = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.tbView.frame.size.width, 60)];
    giftv.backgroundColor =[UIColor whiteColor];
    
    _giftNum = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, giftv.frame.size.width-110, 40)];
    _giftNum.placeholder = @" 请输入优惠券兑换码";
    [_giftNum setBorderStyle:UITextBorderStyleRoundedRect];
    _giftNum.font = FONT(13);
    _giftNum.autocorrectionType = UITextAutocorrectionTypeNo;
    _giftNum.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _giftNum.returnKeyType = UIReturnKeyDone;
    _giftNum.clearButtonMode = UITextFieldViewModeWhileEditing;
    _giftNum.delegate=self;
    [giftv addSubview:_giftNum];
    [_giftNum becomeFirstResponder];
   
    UIButton *useBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    useBtn.frame =CGRectMake(giftv.frame.size.width-90, 10,80, 40);
    useBtn.backgroundColor =RED_COLOR;
    useBtn.tag =2;
    useBtn.titleLabel.font =FONT(15);
    useBtn.layer.cornerRadius = 3;
    useBtn.layer.masksToBounds=YES;
    [useBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [useBtn setTitle:@"使用" forState:UIControlStateNormal];
    useBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    [useBtn addTarget:self action:@selector(usebtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [giftv addSubview:useBtn];
    
    return giftv;
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}
-(void)usebtnClick:(id)sender
{
    [self discountCode:self.giftNum.text];
  
}
-(void)userOk:(NSDictionary *)dict
{
    if (self.actionBlock) {
        self.actionBlock(self.view,dict);
    }
    if (self.navigationController) {
        if (self.navigationController.viewControllers.count == 1) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
-(void)discountCode:(NSString*)code
{
    if (code==nil || code.length==0) {
        [[ZToastManager ShardInstance] showtoast:@"请输入优惠券兑换码"];
        return;
    }
     NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [dictionary setValue:code forKey:@"order_dp_code"];
    [dictionary setValue:_totalfee forKey:@"total_fee"];
    __weak typeof (self) wSelf = self;
    [[ZToastManager ShardInstance] showprogress];
    [OrderApi getDiscountCode:dictionary success:^(AFHTTPRequestOperation *operation, id result) {
        [[ZToastManager ShardInstance] hideprogress];
        if (result&&[result isKindOfClass:[NSDictionary class]]) {
            NSDictionary *resultDist =(NSDictionary*)result;
            int code =[[resultDist objectForKey:@"code"] intValue];
            NSString *msg =[NSString stringWithFormat:@"%@",[resultDist objectForKey:@"msg"]];
            if (msg.length>0) {
                 [[ZToastManager ShardInstance] showtoast:msg];
            }
           
            if (code==1) {
                [wSelf userOk:resultDist];
            }
        }
    } failure:^(NSError *error) {
        [[ZToastManager ShardInstance] hideprogress];
    }];
}
@end
